<?php

namespace app\common\library\supply\engine;

use think\Cache;

/**
 * 充值供应商
 */
class Tao extends Server
{
    const APP_ID = "a1f1wgv3039vc75x";
    const APP_SECRET = "f4f9d3761ee53020251386f01eaaf321";
    const NOTIFY_URL = "http://a.steamliwu.com/prod-api/index.php/shop/order.order/taoNotify";
    const CREATE_ORDER = "http://47.96.156.116";
    private $config;
    private $sendData;

    public function __construct($config)
    {
        parent::__construct();
        $this->config = $config;
    }

    /**
     * 发送直值订单
     */
    public function justRecharge()
    {
        $param = [
            'commodityId'           => $this->sendData["commodityId"],
            'inventoryType'         => 1,
            'username'              => $this->sendData["username"],
            'outOrderId'            => $this->sendData["outOrderId"],
//            'notifyUrl'             => 'http://4gege7.natappfree.cc/index.php/shop/order.order/taoNotify',
            'notifyUrl'             => self::NOTIFY_URL,
        ];

        $time = $this->sendData["time"];
        $appId = self::APP_ID;
        $createOrder = self::CREATE_ORDER;
        $signature = hash_hmac("sha256", "appId={$appId}@{$time}",self::APP_SECRET);
        $headerData = [
            "Accept: application/json",
            "Content-type: application/json",
            "X-Signature: {$signature}",
            "X-Timestamp: {$time}"
        ];
        $result = $this->post($createOrder . "/api/open/orders?appId={$appId}", $param, $headerData);
        $result = json_decode($result);
        mylog("justRecharge", ["请求参数" => $param, "请求头" => $headerData, "返回数据" => $result, "请求地址" => request()->baseUrl()]);
        if ($result->code != 200){
            $this->error = $result->msg ?? "";
            return false;
        }
        return $result;
    }


    public function sendData($param)
    {
        $this->sendData = $param;
    }
}
